<template>
  <el-dialog
    title="活动列表"
    v-model="visible"
    width="1150px"
    @close="onClose"
    :close-on-click-modal="false"
    :lock-scroll="false"
  >
    <div class="main">
      <el-form :model="queryParams" ref="queryRef" :inline="true">
        <el-row>
          <el-form-item>
            <el-button type="primary" icon="Plus" @click="handleAdd">新建</el-button>
            <el-button @click="resetQuery">刷新</el-button>
            <el-button type="primary" @click="handleQuery">搜索</el-button>
          </el-form-item>
          <el-form-item prop="type" label="类型">
            <el-select
              style="width: 160px"
              v-model="queryParams.type"
              clearable
              placeholder="请选择"
            >
              <el-option
                v-for="item in typeOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item prop="name" label="活动名称">
            <el-input
              style="width: 200px"
              v-model="queryParams.name"
              placeholder="请输入活动名称"
              clearable
              @keyup.enter="handleQuery"
            >
            </el-input>
          </el-form-item>
          <el-form-item prop="remark" label="活动备注">
            <el-input
              style="width: 200px"
              v-model="queryParams.remark"
              placeholder="请输入活动备注"
              clearable
              @keyup.enter="handleQuery"
            >
            </el-input>
          </el-form-item>
        </el-row>
      </el-form>
      <el-table
        ref="taskTableRef"
        v-loading="loading"
        size="default"
        index
        :data="dataList"
        @select="selectClick"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column label="活动名称" align="center" prop="name" />
        <el-table-column label="备注" align="center" prop="remark" width="100px" />
        <el-table-column label="类型" align="center" prop="type" width="100px">
          <template #default="{ row }">
            <span>{{ typeOptions?.find((i) => i?.value === row?.type)?.label }}</span>
          </template>
        </el-table-column>
        <el-table-column label="活动时间" min-width="300px" align="center">
          <template #default="{ row }">
            <span>{{ row.startTime ? row.startTime + '~' + row.endTime : '' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="status">
          <template #default="{ row }">
            <span> {{ statusOptions?.find((i) => i?.value === row?.status)?.label }}</span>
          </template>
        </el-table-column>
      </el-table>
      <div style="margin-right: 10px">
        <pagination
          v-show="total > 0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
    </div>
    <template #footer>
      <el-button :loading="submit" type="primary" @click="handleAction(true)"> 保存 </el-button>
      <el-button @click="handleAction(false)">取消</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { getAllActivityListApi } from '@/api/advert'
// 1满减券2折扣券3单品券4新人券5霸王免单券 85打卡 86抽奖 87活动 88礼包
const typeOptions = [
  { value: 1, label: '满减券' },
  { value: 2, label: '折扣券' },
  { value: 3, label: '单品券' },
  { value: 4, label: '新人券' },
  { value: 5, label: '霸王免单券' },
  { value: 85, label: '打卡' },
  { value: 86, label: '抽奖' },
  { value: 87, label: '活动' },
  { value: 88, label: '礼包' }
]
const statusOptions = [
  { value: 1, label: '进行中' },
  { value: 4, label: '待开始' }
]
const router = useRouter()

const emit = defineEmits(['select'])
const queryRef = ref(null)
const taskTableRef = ref(null)

const total = ref(0)
const dataList = ref([])
const visible = ref(false)
const submit = ref(false)

const queryParams = reactive({
  name: '',
  position: '',
  remark: '',
  cityName: '',
  pageNum: 1,
  pageSize: 10
})
const loading = ref(true)

const selectClick = (selection) => {
  if (selection.length > 1) {
    taskTableRef.value.toggleRowSelection(selection.shift(), false)
  }
}

/** 查询全部进行中活动列表 */
function getList() {
  loading.value = true
  getAllActivityListApi(queryParams)
    .then((res) => {
      console.log(res)
      loading.value = false
      dataList.value = res?.rows || []
      total.value = res?.total || 0
    })
    .catch(() => {
      loading.value = false
      dataList.value = []
      total.value = 0
    })
}
// 新建
function handleAdd() {
  router.push({
    path: '/market/tool',
    query: { cityName: queryParams.cityName }
  })
}

// 刷新
function resetQuery() {
  queryRef.value.resetFields()
  handleQuery()
}

// 搜索
function handleQuery() {
  queryParams.pageNum = 1
  getList()
}

function onClose() {
  submit.value = false
  queryRef.value.resetFields()
  queryParams.pageNum = 1
  visible.value = false
}

function handleAction(confirm) {
  if (confirm) {
    if (!taskTableRef.value.getSelectionRows().length) {
      return ElMessage({
        message: '请至少选择一个活动',
        type: 'error'
      })
    }
    onClose()
    emit('select', taskTableRef.value.getSelectionRows()[0])
  } else {
    onClose()
  }
}

function init(id, position, cityName) {
  visible.value = true
  queryParams.id = id
  queryParams.position = position
  queryParams.cityName = cityName
  getList()
}

defineExpose({
  init
})
</script>

<style lang="scss" scoped>
:deep(.pagination-container .el-pagination) {
  right: 20px;
}
.main {
  position: relative;
  transform: translate3d(0, -30px, 0);
}
.type {
  margin-bottom: 20px;
}
:deep(.el-table th.el-table__cell:nth-child(1) .cell) {
  visibility: hidden;
}
</style>
